﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFPrism.Common.Models;
using WPFPrism.Extensions;
using WPFPrism.Views;

namespace WPFPrism.ViewModels
{
    public class MainViewModel : BindableBase
    {
        //注意：
        //1.前台的CommandParameter参数是拿窗体

        /// <summary>
        /// 缩小命令
        /// </summary>
        public DelegateCommand<System.Windows.Window> MinCommand { get; set; }
        /// <summary>
        /// 放大命令
        /// </summary>
        public DelegateCommand<System.Windows.Window> MaxCommand { get; set; }
        /// <summary>
        /// 关闭命令
        /// </summary>
        public DelegateCommand<System.Windows.Window> CloseCommand { get; set; }

        /// <summary>
        /// 导航命令
        /// </summary>
        public DelegateCommand<MenuBar> NavigateCommand { get; set; }

        /// <summary>
        /// 上一步
        /// </summary>
        public DelegateCommand GoBackCommand { get; set; }

        /// <summary>
        /// 下一步
        /// </summary>
        public DelegateCommand GoForwardCommand { get; set; }

        /// <summary>
        /// 点击后，收缩命令
        /// </summary>
        public DelegateCommand<ContentControl> ExpandCommand { get; set; }

        /// <summary>
        /// 菜单集合
        /// </summary>
        private ObservableCollection<MenuBar> menuBars;

        /// <summary>
        /// 区域显示
        /// </summary>
        private readonly IRegionManager regionManager;

        /// <summary>
        /// 区域导航日志
        /// </summary>
        private IRegionNavigationJournal journal;

        /// <summary>
        /// 菜单集合的变化，加载的时候跟着变化
        /// </summary>
        public ObservableCollection<MenuBar> MenuBars
        {
            get { return menuBars; }
            set { menuBars = value; RaisePropertyChanged(); }
        }

        public MainViewModel(IRegionManager regionManager)//初始化
        {
            MenuBars = new ObservableCollection<MenuBar>();
            CreateMenuBar();
            NavigateCommand = new DelegateCommand<MenuBar>(Navigate);

            MinCommand = new DelegateCommand<System.Windows.Window>(SetMin);
            MaxCommand = new DelegateCommand<System.Windows.Window>(SetMax);
            CloseCommand = new DelegateCommand<System.Windows.Window>(SetClose);
            ExpandCommand = new DelegateCommand<ContentControl>(Expand);
            GoBackCommand = new DelegateCommand(() =>
            {
                if (journal != null && journal.CanGoBack)
                {
                    journal.GoBack();
                }
            });

            GoForwardCommand = new DelegateCommand(() =>
            {
                if (journal != null && journal.CanGoForward)
                {
                    journal.GoForward();
                }
            });
            this.regionManager = regionManager;
        }

        private void Expand(ContentControl content)
        {
            ((DrawerHost)content).IsLeftDrawerOpen = false;
        }

        /// <summary>
        /// 导航集合
        /// </summary>
        /// <param name="obj"></param>
        private void Navigate(MenuBar obj)
        {
            if (obj == null || string.IsNullOrWhiteSpace(obj.NameSpace))
            {
                return;
            }
            regionManager.Regions[PrismManager.MainViewRegionName].RequestNavigate(obj.NameSpace, back =>
             {
                 journal = back.Context.NavigationService.Journal;
             });


        }

        private void CreateMenuBar()
        {
            MenuBars.Add(new MenuBar() { Icon = "Home", NameSpace = "IndexView", Title = "首页" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookOutline", NameSpace = "ToDoView", Title = "待办事项" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookEditOutline", NameSpace = "MemoView", Title = "备忘录" });
            MenuBars.Add(new MenuBar() { Icon = "CogOutline", NameSpace = "SettingsView", Title = "设置" });
        }

        private void SetClose(Window win)
        {
            //var dialogResult = await dialogHostService.Question("温馨提示", "确认退出系统?");
            //if (dialogResult.Result != Prism.Services.Dialogs.ButtonResult.OK) return;
            //this.Close();


            win.Close();

        }

        private void SetMax(Window win)
        {
            if (win.WindowState == WindowState.Maximized)
            {
                win.WindowState = WindowState.Normal;
            }
            else
            {
                win.WindowState = WindowState.Maximized;
            }
        }

        private void SetMin(Window win)
        {

            win.WindowState = WindowState.Minimized;
        }

        public void Mian_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (e.Source.ToString().Contains("WPFPrism.Views.MainView"))
                {
                    ((Window)e.Source).DragMove();
                }
                else if (e.Source.ToString().Contains("MaterialDesignThemes.Wpf.ColorZone"))
                {
                    object win = (FrameworkElement)((FrameworkElement)((FrameworkElement)((FrameworkElement)((FrameworkElement)e.Source).Parent).Parent).Parent);
                    ((Window)((FrameworkElement)win).Parent).DragMove();
                }
                //目前先2种，点击到其他控件上面不移动
            }
        }
        public void Mian_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (((Window)e.Source).WindowState == WindowState.Maximized)
            {
                ((Window)e.Source).WindowState = WindowState.Normal;
            }
            else
            {
                ((Window)e.Source).WindowState = WindowState.Maximized;
            }
        }
    }
}
